#define _CRT_SECURE_NO_WARNINGS
#include"多项式相加.h"
NodeList* BuyNode(int x)
{
	NodeList* newnode = (NodeList*)malloc(sizeof(NodeList));
	if (newnode == NULL)
	{
		perror("malloc fail\n");
		return NULL;
	}
	else
	{
		newnode->data = x;
		newnode->next = NULL;
		return newnode;
	}
}

void PushBack(NodeList* head, int x)
{
	NodeList* tail = head->next;
	if (tail == NULL)
	{
		head->next = BuyNode(x);
	}
	else
	{
		while (tail->next)
		{
			tail = tail->next;
		}
		tail->next = BuyNode(x);
	}
}

void DisPlay(NodeList* head)
{
	NodeList* cur = head->next;
	int count = 0;
	while (cur)
	{
		if (count == 0)
		{
			printf("%dx", cur->data);
		}
		else
		{
			printf("+%d", cur->data);
		}
		cur = cur->next;
		count++;
	}
}

void 	 Add(NodeList* head1, NodeList* head2, NodeList* total)
{
	NodeList* cur1 = head1->next;
	NodeList* cur2 = head2->next;
	while (cur1 && cur2)
	{
		PushBack(total, cur1->data + cur2->data);
		cur1 = cur1->next;
		cur2 = cur2->next;
	}
}

int main()
{
	NodeList* head1 = BuyNode(-1);
	NodeList* head2 = BuyNode(-1);
	NodeList* total = BuyNode(-1);
	int n = 0;
	int m = 0;
	int p = 0;
	int q = 0;
	printf("请输入第一个x项系数:");
	scanf("%d", &n);
	printf("请输入第一个的常数项系数:");
	scanf("%d", &m);
	PushBack(head1,n);
	PushBack(head1, m);
	printf("第一个式子：");
	DisPlay(head1);
	printf("\n");
	printf("请输入第二个x项系数:");
	scanf("%d", &p);
	printf("请输入第二个常数项:");
	scanf("%d", &q);
	PushBack(head2, p);
	PushBack(head2, q);
	printf("第二个式子：");
	DisPlay(head2);
	printf("\n");
	printf("相加结果:");
	Add(head1, head2, total);
	DisPlay(total);
	return 0;
}